using System.Web.Services;
using System.Security;
using System.Xml;
using System;
using umlungu.engineX.FtpX;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (Pty) ltd
	Author:		Alan Benington	
	Started:	2005-05-15	
	Status:		release	
	Version:	2.0.0
	Build:		20071203
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	-----------------------------------------------------------------------	
	Development Notes:
	==================
	20070524:	Modification for .Net 2.0
	20071203:	Removed the Test() - it conflicts with x_result.Test
	-----------------------------------------------------------------------	*/

namespace umlungu.engineX.blogX {
	/// <summary>
	/// umlungu EngineX.BlogX 
	/// This module is a component of the EngineX blogging and content management engine
	/// </summary>
	[WebService(Namespace="http://www.umlungu.com/engineX/")]
	public class defaultX : x_result
	{
		/// <summary>
		/// Local objects to access internal methods
		/// </summary>
		#region Private methods
		x_user userx;
		x_blog blogx;
		x_blogspace blogspacex;
		#endregion

		#region Protected methods
		/// <summary>Constructor</summary>
		public defaultX() : base("blogX") {
			userx = new x_user(Application);
			blogx = new x_blog(Application);
			blogspacex = new x_blogspace(Application);
		}
		#endregion

		#region General web service methods
		
		/// <summary>
		/// Logs a user on to the system
		/// </summary>
		/// <remarks>NB: this is the only method that is not validated</remarks>
		[WebMethod] public XmlDocument Login(string username, string password) {
			try {
				userx.Login(username, password);
				string tok = userx.Token;
				XmlElement spaceel, spacesel;
				spacesel = (XmlElement) userx.User.SelectSingleNode("//blogspaces");
				XmlNodeList spaces = spacesel.SelectNodes("blogspace");
				foreach (XmlNode space in spaces) {
					spaceel = (XmlElement) space;
					blogspacex.Get(spaceel.GetAttribute("name"));
					space.InnerText = blogspacex.Blogspace.SelectSingleNode("//title").InnerText;
					// Now get the blogs for default
					if (spaceel.GetAttribute("name") == spacesel.GetAttribute("default")) {
						blogx.List(spaceel.GetAttribute("name"), "default");
						space.InnerXml = space.InnerXml + blogx.Blogs.InnerXml;
						spaceel.SetAttribute("topic", "default");
					}
				}
				_AddOk(userx.User.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(SecurityException e) 	{_AddError(e);}
			catch(Exception e) 			{_AddError(e);}
			return(Result);
		}
		
		/// <summary>
		/// Gets a specific blog
		/// </summary>
		[WebMethod] public XmlDocument GetBlog(string name, string archive, string topic, int id) {
			try {
				topic = (topic == "" || topic == "null")? "default" : topic;
				if (archive == "" || archive == "null" || archive == "current")
					blogx.Get(name, topic, id, true, true);
				else
					blogx.Get(name, topic, archive, id, true, true);
				_AddOk(blogx.Blogs.InnerXml);
//				_AddOk(blogx.Blog.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		
		/// <summary>
		/// Gets blogs specified in the delimited list (delimited by ',:; |')
		/// </summary>
		[WebMethod] public XmlDocument GetBlogs(string name, string archive, string topic, string list) {
			try {
				topic = (topic == "" || topic == "null")? "default" : topic;
				if (archive == "" || archive == "null" || archive == "current")
					blogx.Gets(name, topic, list, true, true);
				else
					blogx.Gets(name, topic, archive, list, true, true);
				_AddOk(blogx.Blogs.InnerXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		
		/// <summary>
		/// Gets all blogs in an archive (or current if 'archive' blank)
		/// </summary>
		[WebMethod] public XmlDocument ListBlogs(string name, string topic, string archive) {
			try {
				topic = (topic == "" || topic == "null")? "default" : topic;
				if (archive == "" || archive == "null" || archive == "current")
					blogx.List(name, topic);
				else
					blogx.List(name, topic, archive);
				_AddOk(blogx.Blogs.InnerXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/*
		/// <summary>
		/// Tests
		/// </summary>
		[WebMethod] public XmlDocument Test(string anything) {
			try {
				_AddOk();
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		*/
		/// <summary>
		/// Register a new blogspace and a new blogadmin user (admin for the space)
		/// </summary>
		[WebMethod] public XmlDocument RegisterSpace(string username, string firstname, string surname, string email, string telno, string secret, string password, string password2, string spacename, string spacetype, string spacetitle, string spacetext) {
			try {
				if (password != password2)
					throw(new x_exception("error_confirm", "password is not confirmed"));
				string defgroup = "blogadmin";
				userx.Add(username, firstname, surname, email, telno, secret, password, defgroup);
				blogspacex.Add(spacename, spacetype, spacetitle, spacetext, username, true, true);
				userx.AddBlogspace(spacename, username, defgroup, true);
				Login(username, password);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>
		/// Register a new author in an existing blogspace
		/// </summary>
		[WebMethod] public XmlDocument RegisterAuthor(string username, string firstname, string surname, string email, string telno, string secret, string password, string password2, string spacename) {
			try {
				if (password != password2)
					throw(new x_exception("error_confirm", "password is not confirmed"));
				string defgroup = "author";
				userx.Add(username, firstname, surname, email, telno, secret, password, defgroup);
				blogspacex.AddUser(spacename, username, defgroup);
				userx.AddBlogspace(spacename, username, defgroup, true);
				Login(username, password);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>
		/// Gets the files from a remote server via ftp
		/// </summary>
		[WebMethod] public XmlDocument GetFiles(string remote_host, string remote_user, string remote_password, string remote_path, string remote_mask) {
			remote_path = (remote_path != "")? remote_path : ".";
			remote_mask = (remote_mask != "")? remote_mask : "*";
			x_ftp ff = new x_ftp(remote_host, remote_path, remote_user, remote_password, 21, true);
			try {
				ff.login();
				ff.chdir(remote_path);
				string[] fileNames = ff.FileList(remote_mask);
				_AddOk();
				for(int i=0;i < fileNames.Length;i++) {
					_Add("file", fileNames[i]);
				}
				_Add("debug", ff.Message);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			if (ff != null)
				ff.close();
			return(Result);
		}
		/// <summary>Gets a blogspace</summary>
		[WebMethod] public XmlDocument GetSpace(string name) {
			try {
				blogspacex.Get(name);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		/// <summary>Gets a list of blogspaces</summary>
		[WebMethod] public XmlDocument GetSpaces() {
			try {
				blogspacex.List();
				_AddOk(blogspacex.Blogspaces.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		
		/// <summary>
		/// Gets all current blogs in a blogspace
		/// </summary>
		[WebMethod] public XmlDocument GetAllBlogs(string name) {
			try {
				blogspacex.CountBlogs(name);	// put the blog count into each topic
				blogspacex.Current(name);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		
		/// <summary>
		/// Gets all archives in a blogspace
		/// </summary>
		[WebMethod] public XmlDocument GetAllArchives(string name) {
			try {
				blogspacex.Archives(name);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		
		
		/// <summary>
		/// Forces an index of a blogspace
		/// </summary>
		[WebMethod] public XmlDocument Index(string name) {
			try {
				blogspacex.Index(name);
				_AddOk(blogspacex.Blogspace.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		
		/// <summary>
		/// Delete a comment
		/// </summary>
		[WebMethod] public XmlDocument DeleteComment(string name, string topic, int id, int cid) {
			try {
				topic = (topic == "")? "default" : topic;
				blogx.DeleteComment(name, topic, id, cid);
				_AddOk(blogx.Blog.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		
		/// <summary>
		/// Approve a comment
		/// </summary>
		[WebMethod] public XmlDocument ApproveComment(string name, string topic, int id, int cid) {
			try {
				topic = (topic == "")? "default" : topic;
				blogx.ApproveComment(name, topic, id, cid);
				_AddOk(blogx.Blog.OuterXml);
			} 
			catch(x_exception e)		{_AddError(e);}
			catch(System.Exception e) 	{_AddError(e);}
			return(Result);
		}
		
		#endregion

	}
}
